<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Redis list 之增删改查 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Redis list 之增删改查</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="一、增加"><a href="#一、增加" class="headerlink" title="一、增加"></a>一、增加</h2><ol>
<li>lpush [lpush key valus…]  类似于压栈操作，将元素放入头部</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpush plist ch0 ch1 ch2</div><div class="line">(<span class="built_in">integer</span>) 3</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 3</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch1"</span></div><div class="line">3) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lpush plist ch4</div><div class="line">(<span class="built_in">integer</span>) 4</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 4</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"ch1"</span></div><div class="line">4) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<ol>
<li>lpushx [lpushx key valus]:只能插入已经存在的key,且一次只能插入一次</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpushx pl ch</div><div class="line">(<span class="built_in">integer</span>) 0</div><div class="line">127.0.0.1:6379&gt; lpushx plist ch5 ch6</div><div class="line">(error) ERR wrong number of arguments <span class="keyword">for</span> <span class="string">'lpushx'</span> <span class="built_in">command</span></div><div class="line">127.0.0.1:6379&gt; lpushx plist ch5</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 5</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<ol>
<li>rpush  [rpush key valus…] ：将元素push在list的尾部</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; rpush plist ch6</div><div class="line">(<span class="built_in">integer</span>) 6</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 6</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div><div class="line">6) <span class="string">"ch6"</span></div><div class="line">127.0.0.1:6379&gt; rpush plist ch7 ch8</div><div class="line">(<span class="built_in">integer</span>) 8</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 8</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"ch1"</span></div><div class="line">5) <span class="string">"ch0"</span></div><div class="line">6) <span class="string">"ch6"</span></div><div class="line">7) <span class="string">"ch7"</span></div><div class="line">8) <span class="string">"ch8"</span></div></pre></td></tr></table></figure>
<ol>
<li><p>rpushx [rpushx key valus…] :相对于lpushx</p>
</li>
<li><p>linsert [linsert key before/after pivot value]:将值插入到pivot的前面或后面。返回列表元素个数。如果参照点pivot不存在不插入。如果有多个pivot，以离表头最近的为准</p>
</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; linsert plist before ch1 chi</div><div class="line">(<span class="built_in">integer</span>) 9</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 9</div><div class="line">1) <span class="string">"ch5"</span></div><div class="line">2) <span class="string">"ch4"</span></div><div class="line">3) <span class="string">"ch2"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div><div class="line">9) <span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; linsert plist before chii chi2</div><div class="line">(<span class="built_in">integer</span>) -1</div><div class="line">127.0.0.1:6379&gt; linsert plist after chi cha</div><div class="line">(<span class="built_in">integer</span>) 10</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch5"</span></div><div class="line"> 2) <span class="string">"ch4"</span></div><div class="line"> 3) <span class="string">"ch2"</span></div><div class="line"> 4) <span class="string">"chi"</span></div><div class="line"> 5) <span class="string">"cha"</span></div><div class="line"> 6) <span class="string">"ch1"</span></div><div class="line"> 7) <span class="string">"ch0"</span></div><div class="line"> 8) <span class="string">"ch6"</span></div><div class="line"> 9) <span class="string">"ch7"</span></div><div class="line">10) <span class="string">"ch8"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以上插入操作均是返回list的长度</p>
<h2 id="二、删除"><a href="#二、删除" class="headerlink" title="二、删除"></a>二、删除</h2><ol>
<li>lpop 、rpop：分别为删除头部和尾部，返回被删除的元素</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lpop plist</div><div class="line"><span class="string">"ch5"</span></div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div><div class="line">9) <span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; rpop plist</div><div class="line"><span class="string">"ch8"</span></div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div><div class="line">5) <span class="string">"ch1"</span></div><div class="line">6) <span class="string">"ch0"</span></div><div class="line">7) <span class="string">"ch6"</span></div><div class="line">8) <span class="string">"ch7"</span></div></pre></td></tr></table></figure>
<ol>
<li>ltrim [ltrim key  range_l range_r]:保留区域类的元素，其他的删除</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; ltrim plist 0 3</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"ch2"</span></div><div class="line">3) <span class="string">"chi"</span></div><div class="line">4) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<ol>
<li>lrem [lrem key count value] :移除等于value的元素，当count&gt;0时，从表头开始查找，移除count个；当count=0时，从表头开始查找，移除所有等于value的；当count&lt;0时，从表尾开始查找，移除|count| 个。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;cout &gt;0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch0"</span></div><div class="line"> 2) <span class="string">"ch0"</span></div><div class="line"> 3) <span class="string">"ch0"</span></div><div class="line"> 4) <span class="string">"ch4"</span></div><div class="line"> 5) <span class="string">"chi"</span></div><div class="line"> 6) <span class="string">"cha"</span></div><div class="line"> 7) <span class="string">"ch0"</span></div><div class="line"> 8) <span class="string">"ch0"</span></div><div class="line"> 9) <span class="string">"ch0"</span></div><div class="line">10) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lrem plist 5 ch0</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch4"</span></div><div class="line">2) <span class="string">"chi"</span></div><div class="line">3) <span class="string">"cha"</span></div><div class="line">4) <span class="string">"ch0"</span></div><div class="line">5) <span class="string">"ch0"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;count &lt; 0</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line"> 1) <span class="string">"ch0"</span></div><div class="line"> 2) <span class="string">"ch9"</span></div><div class="line"> 3) <span class="string">"ch0"</span></div><div class="line"> 4) <span class="string">"ch0"</span></div><div class="line"> 5) <span class="string">"ch0"</span></div><div class="line"> 6) <span class="string">"ch4"</span></div><div class="line"> 7) <span class="string">"chi"</span></div><div class="line"> 8) <span class="string">"cha"</span></div><div class="line"> 9) <span class="string">"ch0"</span></div><div class="line">10) <span class="string">"ch0"</span></div><div class="line">127.0.0.1:6379&gt; lrem plist -5 ch0</div><div class="line">(<span class="built_in">integer</span>) 5</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch0"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<h2 id="三、修改"><a href="#三、修改" class="headerlink" title="三、修改"></a>三、修改</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;lset [lset key index value] : 设置列表指定索引的值，如果指定索引不存在则报错</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lset plist 0 ch2</div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div></pre></td></tr></table></figure>
<h2 id="四、查询"><a href="#四、查询" class="headerlink" title="四、查询"></a>四、查询</h2><ol>
<li>lindex [lindex key index]:通过索引index获取列表的元素、key&gt;=0从头到尾，key&lt;0从尾到头</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">127.0.0.1:6379&gt; lrange plist 0 10</div><div class="line">1) <span class="string">"ch2"</span></div><div class="line">2) <span class="string">"ch9"</span></div><div class="line">3) <span class="string">"ch4"</span></div><div class="line">4) <span class="string">"chi"</span></div><div class="line">5) <span class="string">"cha"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist 0</div><div class="line"><span class="string">"ch2"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist -0</div><div class="line"><span class="string">"ch2"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist -1</div><div class="line"><span class="string">"cha"</span></div><div class="line">127.0.0.1:6379&gt; lindex plist 5</div><div class="line">(nil)</div></pre></td></tr></table></figure>
<ol>
<li>lrange [lrange key range_l range_r]: 0表头、-1表尾</li>
</ol>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/7. Redis list 之增删改查/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
